package com.rigsofrods.truckeditor.gui.views;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import com.rigsofrods.truckeditor.control.CameraController;
import com.rigsofrods.truckeditor.geometry.Scene;
import com.rigsofrods.truckeditor.gui.GLViewport;
import com.rigsofrods.truckeditor.gui.AbstractViewport.Projection;
import com.rigsofrods.truckeditor.transforms3D.Camera;
import com.rigsofrods.truckeditor.transforms3D.Camera.PresetView;


public class Truck3dView extends JPanel{
	private static final long serialVersionUID = 401012769511656104L;
	//Viewport size
	public static final int WIDTH = 480;
	public static final int HEIGHT = 320;
	private Border brdViewportInactive = BorderFactory.createMatteBorder(1, 1, 1, 1,Color.GRAY);
	//private Border brdViewportActive = BorderFactory.createMatteBorder(1, 1, 1, 1,Color.YELLOW);
	private GLViewport[] pnlViewport = new GLViewport[4];
	protected GLViewport activeViewport;
	private Scene scene;
	
	public Truck3dView() {
		//TODO: Viewports have a scene change listener and redraw themselves
		super(new GridBagLayout());
		setPreferredSize(new Dimension((2 * WIDTH) + 3, (2 * HEIGHT) + 3));
		//setMinimumSize(minSize);
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		// TODO Improve Borders
		// -- MouseAdapter ----------------------------------
		CameraController camCtrl = new CameraController();

		// --create viewports ---------------------
		Dimension minSize = new Dimension(240, 160); 
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.PAGE_START;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 0.5;
		for (int i = 0; i < pnlViewport.length; i++) {
			pnlViewport[i] = new GLViewport(WIDTH, HEIGHT);
			pnlViewport[i].setMinimumSize(minSize);
			pnlViewport[i].setPreferredSize(new Dimension(WIDTH, HEIGHT));
			pnlViewport[i].setBorder(brdViewportInactive);
			// pnlViewport[i].setBorder(BorderFactory.createMatteBorder(1, 5, 1, 1, Color.red));
			// JPanel pnlVPContainer = new JPanel();
			/*pnlVPContainer.setLayout(new BoxLayout(pnlVPContainer, BoxLayout.X_AXIS));
			pnlVPContainer.setBorder(brdViewportInactive);
			pnlVPContainer.add(pnlViewport[i]);
			add(pnlVPContainer);//*/
			pnlViewport[i].addMouseListener(camCtrl);
			pnlViewport[i].addMouseMotionListener(camCtrl);
			pnlViewport[i].addMouseWheelListener(camCtrl);
			c.gridx = i & 1;//1st bit of i
			c.gridy = i & 2;
			add(pnlViewport[i], c);
		}
		pnlViewport[0].setCamera(new Camera(PresetView.FRONT_VIEW));
		pnlViewport[0].setProjection(Projection.ORTHOGONAL_PROJECTION);
		pnlViewport[1].setCamera(new Camera(PresetView.LEFT_VIEW));
		pnlViewport[1].setProjection(Projection.ORTHOGONAL_PROJECTION);
		pnlViewport[2].setCamera(new Camera(PresetView.TOP_VIEW));
		pnlViewport[2].setProjection(Projection.ORTHOGONAL_PROJECTION);
		pnlViewport[3].setCamera(new Camera(PresetView.ISO_VIEW));
	}
	
	/*private void redrawViewports() {
		for (int i = 0; i < pnlViewport.length; i++) { pnlViewport[i].drawScene(); }
	}*/

	public Scene getScene() {
		return scene;
	}

	public void setScene(Scene scene) {
		this.scene = scene;
		for (GLViewport vp : pnlViewport) {
			vp.setScene(scene);
		}
	}
}
